import 'package:sqflite/sqflite.dart';

export 'package:sqflite/sqflite.dart';

class Db {
  Future<Database> getDatabase() async {
    await initTables();
    var databasesPath = await getDbPath();
    Database database = await openDatabase(databasesPath);
    return database;
  }

  Future<String> getDbPath() async {
    var databasesPath = await getDatabasesPath();
    String path = databasesPath + "message_tools.db";
    return path;
  }

  void initTables() async {
    String path = await getDbPath();
    await openDatabase(path, version: 1,
        onCreate: (Database db, int version) async {
      await db.execute('''
           create table sms_template (
        id integer primary key autoincrement,
        title varchar not null,
        content varchar not null,
        created_at varchar);''');

      await db.execute('''
           create table sms_task (
        id integer primary key autoincrement,
        title varchar not null,
        sms_template_title varchar not null,
        sms_template_content varchar not null,
        contact_file_path varchar not null,
        created_at varchar);''');

      await db.execute('''
           create table sms_send_record (
        id integer primary key autoincrement,
        nindex varchar,
        phone_num varchar not null,
        sms_task_id varchar,
        content varchar not null,
        status integer);''');
    });
  }
}
